package com.lanyan.system.controller.api;

import com.lanyan.common.annotation.RepeatSubmit;

import java.math.BigDecimal;

import com.lanyan.common.utils.SecurityUtils;
import com.lanyan.common.utils.bean.BeanUtils;
import com.lanyan.system.domain.Account;
import com.lanyan.system.domain.Coupon;
import com.lanyan.system.service.ICouponService;
import io.swagger.annotations.Api;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Date;

import com.lanyan.common.core.domain.R;
import com.lanyan.common.core.domain.PageR;

import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lanyan.common.annotation.Log;
import io.swagger.annotations.ApiOperation;
import com.lanyan.common.core.controller.BaseController;
import com.lanyan.common.core.domain.AjaxResult;
import com.lanyan.common.core.domain.R;
import com.lanyan.common.enums.BusinessType;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import springfox.documentation.annotations.ApiIgnore;
import com.lanyan.common.utils.StringUtils;
import com.lanyan.system.domain.AccountCoupon;
import com.lanyan.system.domain.vo.AccountCouponVo;
import com.lanyan.system.service.IAccountCouponService;
import com.lanyan.common.utils.poi.ExcelUtil;
import com.lanyan.common.core.page.TableDataInfo;

/**
 * 用户优惠券Controller
 *
 * @author lanyan
 * @date 2024-11-26
 */
@RestController
@Api(tags = {"用户优惠券"})
@RequestMapping("/api/accountCoupon")
public class ApiAccountCouponController extends BaseController {
    @Autowired
    private IAccountCouponService accountCouponService;
    @Autowired
    private ICouponService couponService;

    /**
     * 查询待领取优惠券
     */
    @GetMapping("/unget")
    @ApiOperation("查询待领取优惠券")
    public R unget() {
        return R.ok(couponService.unget(SecurityUtils.getUserId()));
    }

    /**
     * 查询用户优惠券列表
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态（0待使用 1已使用 2已过期）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageNum", value = "第几页", dataType = "int", dataTypeClass = int.class),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", dataTypeClass = int.class),
    })
    @ApiOperation("查询用户优惠券列表")
    public R<List<AccountCoupon>> list(@ApiIgnore AccountCoupon accountCoupon) {
        accountCouponService.lambdaUpdate()
                .eq(AccountCouponVo::getAccountId, SecurityUtils.getUserId())
                .le(AccountCouponVo::getTimer, new Date())
                .set(AccountCouponVo::getState, "2")
                .update();
        accountCoupon.setAccountId(SecurityUtils.getUserId());
        List<AccountCoupon> list = accountCouponService.selectAccountCouponList(accountCoupon);
        return R.ok(list);
    }

    /**
     * 获取用户优惠券详细信息
     */
    @GetMapping(value = "/info")
    @ApiOperation("获取用户优惠券详细信息")
    public R<AccountCoupon> getInfo(Long accountCouponId) {
        return R.ok(accountCouponService.selectAccountCouponByAccountCouponId(accountCouponId));
    }

    /**
     * 新增用户优惠券
     */
    @Log(title = "用户优惠券", businessType = BusinessType.INSERT)
    @PostMapping("save")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "couponId", value = "所属优惠券", dataType = "Long", dataTypeClass = Long.class),
    })
    @RepeatSubmit
    @ApiOperation("新增用户优惠券")
    public R<AccountCoupon> add(@ApiIgnore @RequestBody AccountCoupon accountCoupon) {
        StringUtils.insertRequired(accountCoupon);
        Coupon coupon = couponService.getById(accountCoupon.getCouponId());
        BeanUtils.copyBeanProp(accountCoupon, coupon);
        accountCoupon.setAccountId(SecurityUtils.getUserId());
        if (StringUtils.equals(coupon.getLoseType(), "2")) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf(coupon.getTimer()));
            accountCoupon.setTimer(sdf.format(calendar.getTime()));
        }
        accountCouponService.save(accountCoupon);
        return R.ok(accountCoupon);
    }

    /**
     * 删除用户优惠券
     */
    @Log(title = "用户优惠券", businessType = BusinessType.DELETE)
    @GetMapping("/del")
    @ApiOperation("删除用户优惠券")
    public AjaxResult remove(Long accountCouponId) {
        accountCouponService.deleteAccountCouponByAccountCouponId(accountCouponId);
        return success("删除成功");
    }
}
